package lu.uni.adtool.domains.rings;

import java.io.Serializable;


/**
 * Set of real numbers in between 0 and 1 (including 0 and 1).
 * 
 * @author Piot Kordy
 */
public class RealZeroOne implements Serializable, Ring
{
  static final long serialVersionUID = 145654787766564466L;
  private double value;
  /**
   * Constructs a new instance.
   */
  public RealZeroOne()
  {
    this(0);
  }

  /**
   * Constructs a newly allocated object that represents the primitive
   * double argument.
   * 
   * @param d the value to be represented by the RealZeroOne.
   */
  public RealZeroOne(final double d)
  {
    value = d;
    if (value <0) {
      value = 0;
    }
    if (value >1) {
      value = 1;
    }
  }

  /**
   * {@inheritDoc}
   * @see Object#toString()
   */
  public final String toString()
  {
    return new Double(value).toString();
  }
  /**
   * {@inheritDoc}
   * @see Ring#toUnicode()
   */
  public final String toUnicode()
  {
    return this.toString();
  }
  /**
   * Return a sum of probabilities.
   * 
   * @param a the first probability
   * @param b the second probability
   * @return the sum of probabilities
   */
  public static final RealZeroOne plusProb(final RealZeroOne a,
      final RealZeroOne b)
  {
    final double x = a.getValue();
    final double y = b.getValue();
    return new RealZeroOne(x+y-x*y);
  }
  /**
   * Return a difference of probabilities.
   * 
   * @param a the first probability
   * @param b the second probability
   * @return the difference of probabilities
   */
  public static final RealZeroOne minusProb(final RealZeroOne a,
      final RealZeroOne b)
  {
    final double x = a.getValue();
    final double y = b.getValue();
    return new RealZeroOne(x*(1-y));
  }
  /**
   * Return a multiplitation of probabilities.
   * 
   * @param a the first probability
   * @param b the second probability
   * @return the result.
   */
  public static final RealZeroOne times(final RealZeroOne a,final RealZeroOne b)
  {
    final double x = a.getValue();
    final double y = b.getValue();
    return new RealZeroOne(x*y);
  }
  /**
   * Gets the value for this instance.
   *
   * @return The value.
   */
  public double getValue()
  {
    return this.value;
  }
}
